﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Robocode;
using Maubot.Motion;
using Maubot.Modeling;
using Maubot.Behavoir;
using Maubot.MathFunctions;

namespace MauBot
{
    public class MauRobot : TeamRobot
    {
        MoveCommand MoveCommand = new MoveCommand();
        Position TargetPosition = new Position();
        Pose RobotPose = new Pose();
        BehavoirModule behavoir;
        MotionModule motion;

        private void Init()
        {
            this.BroadcastMessage("I'm alive");

            RobotPose.X = this.X;
            RobotPose.Y = this.Y;
            TargetPosition.X = RobotPose.X;
            TargetPosition.Y = RobotPose.Y;

            behavoir = new BehavoirModule(RobotPose, TargetPosition);
            motion = new MotionModule(RobotPose, TargetPosition, MoveCommand);

            this.IsAdjustGunForRobotTurn = true;
            this.IsAdjustRadarForGunTurn = true;
            this.IsAdjustRadarForRobotTurn = true;
        }

        public override void Run()
        {
            Init();

            this.BroadcastMessage("Ready");

            while (true)
            {
                RobotPose.X = this.X;
                RobotPose.Y = this.Y;
                RobotPose.Angle = this.Heading;

                behavoir.Execute();
                motion.Execute();

                this.SetAhead(MoveCommand.Speed);
                this.SetTurnRight(MoveCommand.Angle);

                if (!seen)
                    this.SetTurnRadarRight(45);
                else
                    seen = false;
                this.Execute();
            }
        }

        public override void OnMessageReceived(MessageEvent evnt)
        {
            this.Out.WriteLine(evnt.Sender + " : " + evnt.Message);
            base.OnMessageReceived(evnt);
        }

        private bool seen;
        public override void OnScannedRobot(ScannedRobotEvent evnt)
        {
            seen = true;

            double enemyAngle = Calcolator.NormalizeAngle(evnt.Bearing + this.Heading, 180);
            double rotation = Calcolator.NormalizeAngle(enemyAngle - this.RadarHeading, 180);
            this.SetTurnRadarRight(rotation);

            if ((enemyAngle - this.GunHeading) <= 1)
                this.SetFire(1);

            this.SetTurnGunRight(Calcolator.NormalizeAngle(enemyAngle - this.GunHeading, 180));
            
            base.OnScannedRobot(evnt);
        }

        public override void OnHitWall(HitWallEvent evnt)
        {
            this.Out.WriteLine("Benf!");
            base.OnHitWall(evnt);
        }

        public override void OnHitRobot(HitRobotEvent evnt)
        {
            this.Out.WriteLine("Sbang!");
            base.OnHitRobot(evnt);
        }

        public override void OnMouseClicked(MouseEvent e)
        {
            this.Out.WriteLine("Click " + e.Button);
            behavoir.OnMouseClicked(e.X, e.Y);
            base.OnMouseClicked(e);
        }
    }
}
